package clienttest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import syferserver.UDPStream;

public class Client extends Thread {

  private Thread inThread;
  private Thread outThread;
  private Socket socSocket;
  private Socket chatSocket;
  int c;

  public Client() {

    try {

      String hostname = JOptionPane.showInputDialog("IP?");
      int port = 55555;
      int channel = 44444;

      chatSocket = new Socket(hostname,port+1);
      PrintWriter schrijver = new PrintWriter(chatSocket.getOutputStream(), true);
      System.out.println("Connecting to " + hostname);

      new Microphone(hostname,channel);
      
      schrijver.println("port:"+UDPStream.getLocalPort());
      schrijver.println("channel:"+channel);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    new Client();
  }

  //Text based chat for test purposes
  class InThread extends Thread {

    @Override
    public void run() {
      try {
        while ((c = socSocket.getInputStream().read()) != -1) {
          System.out.write(c);
        }
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
  class OutThread extends Thread {

    @Override
    public void run() {

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      try {
        while ((c = in.read()) != -1) {
          socSocket.getOutputStream().write(c);
        }
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }
}
